import keras as K
import keras.callbacks as callbacks
import numpy as np
import load3

fuzhai=[]
shouyi=[]
wenti=[]
zhouzhuan=[]
huitou=[]
y=[]

for i in range(1,124):
    fuzhai.append(float(load3.getCell(i,load3.fuzhai)))
    shouyi.append(float(load3.getCell(i,load3.shouyi)))
    wenti.append(float(load3.getCell(i,load3.wenti)))
    zhouzhuan.append(float(load3.getCell(i,load3.zhouzhuan)))
    huitou.append(float(load3.getCell(i,load3.huitou)))
    y.append(float(load3.getCell(i,load3.y)))

encoded_Y = K.utils.to_categorical(y)
# print(encoded_Y)
x=[fuzhai,shouyi,wenti,zhouzhuan,huitou]
x=np.array(x).T
# shuffle
index = np.arange(123)
np.random.shuffle(index)
x = x[index]
encoded_Y = encoded_Y[index]
print(x)
print(encoded_Y)

print(x.shape)

callback_list = [
        callbacks.ModelCheckpoint(filepath="model_1.h5", monitor="val_accuracy", save_best_only=True),
        callbacks.ReduceLROnPlateau(monitor="val_loss", factor=0.7, verbose=1, patience=1000)
    ]

model = K.models.Sequential()
model.add(K.layers.Dense(units=32, input_dim=5, activation='relu'))
model.add(K.layers.Dense(units=64, activation='relu'))
model.add(K.layers.Dense(units=32, activation='relu'))
model.add(K.layers.Dense(units=4, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x, encoded_Y, batch_size=123, epochs=50000, callbacks=callback_list, validation_split=0.1)
model.save('pingji.h5')